package cn.wolfcode.luowowo.mgrsite.web.job;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

//数据持久化 redis ---> mysql
//@Component
public class RedisDataPersistenceJob {

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategyDetailService detailService;

    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void dowork(){
        System.out.println("--持久化开始--");
        //拿到所有的key  keys strategy_statis_vo:*
        String key = RedisKeys.STRATEGY_STATIS_VO.join("*");
        //从redis中获取所有的VO对象
        List<StrategyStatisVO> list = strategyStatisRedisService.listStratrgyStatisVO(key);

        //将数据更新到mysql中
        for (StrategyStatisVO vo : list) {
            StrategyStatisPersistenceVO pvo = new StrategyStatisPersistenceVO();
            BeanUtil.copyProperties(pvo,vo);
            detailService.updatePVO(pvo);
        }
        System.out.println("--持久化结束--");
    }
}
